package blackberry.util.field;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.TouchEvent;

public class DjChangeableBitmapField extends DjNonHighlightBitmapField {
	
	protected Vector bitmapVector = new Vector(2); 
	
	private boolean enabled = true;
	
	public DjChangeableBitmapField(Bitmap[] bitmapArray, long style) {
		super(bitmapArray[0], style);
		
		bitmapVector = new Vector(bitmapArray.length);
		
		for (int i = 0; i < bitmapArray.length; i++) {
			bitmapVector.addElement(bitmapArray[i]);
		}
	}

	public void changeBitmapField(int index){
		try {
			setBitmap((Bitmap) bitmapVector.elementAt(index));
		} catch (Exception e) {
		}
	}

	protected boolean navigationUnclick(int status, int time) {
		if (enabled == false || status == 0)
			return false;
		execute();
		return super.navigationUnclick(status, time);
	}
	
	protected boolean touchEvent(TouchEvent touchevent) {
		if (enabled == false)
			return false;
		if (touchevent.getEvent() == TouchEvent.CLICK) {
			if (outOfBounds(touchevent))
				return false;
			execute();
		}
		return super.touchEvent(touchevent);
	}
	
	protected boolean outOfBounds(TouchEvent touchevent){
		return touchevent.getX(1) < 0 || touchevent.getY(1) < 0 ||
				touchevent.getX(1) > getWidth() || touchevent.getY(1) > getHeight();
	}

	protected boolean keyUp(int keycode, int time) {
		if (enabled == false)
			return false;
		
		if (isExecuteKey(keycode)) {
			execute();
		}
		return super.keyDown(keycode, time);
	}
	
	protected boolean isExecuteKey(int keycode) {
		return Keypad.key(keycode) == Keypad.KEY_ENTER;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public void execute(){};
}
